//
//  CustomFontVC.swift
//  Swift_Study
//
//  Created by apple on 2018/9/26.
//  Copyright © 2018年 dminfo. All rights reserved.
//

import UIKit

class CustomFontVC: UIViewController {
  
  
  var data = ["30 Days Swift", "这些字体特别适合打「奋斗」和「理想」", "谢谢「造字工房」，本案例不涉及商业使用", "使用到造字工房劲黑体，致黑体，童心体", "呵呵，再见🤗 See you next Project", "微博 @Allen朝辉",
              "测试测试测试测试测试测试",
              "123",
              "Alex",
              "@@@@@@"]
   var fontNames = ["MFTongXin_Noncommercial-Regular", "MFJinHei_Noncommercial-Regular", "MFZhiHei_Noncommercial-Regular", "Zapfino", "Gaspar Regular"]
   var fontRowIndex = 0
  var tbvFont = UITableView.init(frame:CGRect(x: 0, y: 0, width: kSize.width, height: kSize.height - 200)
    , style: .plain)
  var changeFontButton = UIButton.init(frame: CGRect(x: (kSize.width - 150)/2, y: kSize.height - 150, width: 150, height: 100)
  
)
  
    override func viewDidLoad() {
        super.viewDidLoad()
      
       configInterface()
      
        
    }
  func configInterface(){
    view.backgroundColor = UIColor.black
    title = "Custom Font"
    tbvFont.backgroundColor = UIColor.black
    tbvFont.delegate = self
    tbvFont.dataSource = self
    tbvFont.separatorStyle = .none
    changeFontButton.setTitle("Change Font", for: .normal)
    changeFontButton.setTitleColor(UIColor.black, for: .normal)
    changeFontButton.layer.cornerRadius = 75
    changeFontButton.backgroundColor = UIColor.yellow
    changeFontButton.addTarget(self, action: #selector(self.changeFontDidTouch), for: .touchUpInside)
    view.addSubview(tbvFont)
    view.addSubview(changeFontButton)
  }
//点击事件
  @objc func changeFontDidTouch(){
    fontRowIndex = (fontRowIndex + 1) % 5
    print(fontNames[fontRowIndex])
    tbvFont.reloadData()
  }
}


// MARK:- tableView -delegate/datasource
extension CustomFontVC:UITableViewDelegate,UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellId = "cellId"
    var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
    if cell == nil {
      cell = UITableViewCell(style:UITableViewCellStyle.default,reuseIdentifier:cellId)
    }
    let text = data[indexPath.row]
    cell?.backgroundColor = UIColor.black
    cell?.textLabel?.text = text
    cell?.textLabel?.textColor = UIColor.white
    cell?.textLabel?.font = UIFont(name:self.fontNames[fontRowIndex], size:16)
    return cell!
  }
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 35
  }
}







